package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.converter.ShoppingHomePageConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.PanelContentItemDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.ShoppingHomePageResponse;
import com.cskaoyan.shopping.service.ShoppingHomePageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.List;

/**
 * 类说明
 *
 * @author zhangfuqiang
 * @date 2022/5/19
 */
@Service
public class ShoppingHomePageServiceImpl implements ShoppingHomePageService {
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ItemMapper itemMapper;

    @Autowired
    ShoppingHomePageConverter shoppingHomePageConverter;

    @Override
    public ShoppingHomePageResponse getShoppingHomePage() {
        ShoppingHomePageResponse shoppingHomePageResponse = new ShoppingHomePageResponse();
        try {
            //拿到大板块查询数据
            List<Panel> panels = panelMapper.selectAll();
            List<PanelDto> panelDtos = shoppingHomePageConverter.panelDos2PanelDtos(panels);



            for (PanelDto panelDto : panelDtos) {

                //得到每个板块下的具体信息
                Integer id = panelDto.getId();
                Example example = new Example(PanelContent.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andEqualTo("panelId",id);
                criteria.andIsNotNull("panelId");
                List<PanelContent> panelContents = panelContentMapper.selectByExample(example);

                List<PanelContentItemDto> panelContentItems = shoppingHomePageConverter.panelContents2PanelContentItems(panelContents);

                for (PanelContentItemDto panelContentItem : panelContentItems) {
                    //得到每个具体信息的商品名，价格和卖点
                    Long productId = panelContentItem.getProductId();
                    if (productId != null){

                        Item item = itemMapper.selectByPrimaryKey(productId);
                        String title = item.getTitle();
                        String sellPoint = item.getSellPoint();
                        BigDecimal price = item.getPrice();
                        panelContentItem.setProductName(title);
                        panelContentItem.setSalePrice(price);
                        panelContentItem.setSubTitle(sellPoint);
                    }
                }

                panelDto.setPanelContentItems(panelContentItems);

            }

            shoppingHomePageResponse.setResponse(panelDtos);
            shoppingHomePageResponse.setTimestamp(System.currentTimeMillis());
            shoppingHomePageResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            shoppingHomePageResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(shoppingHomePageResponse, e);
        }

        return shoppingHomePageResponse;
    }
}
